/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.ui;

import ac.lib.users.permissions;
import static ac.settings.dataModel.tableAddstree;
import static ac.settings.dataModelurls.URL_Image;
import ac.ui.controls.PCLButton;
import ac.ui.controls.PCLCheckBox;
import ac.ui.controls.PCLDate;
import ac.ui.controls.PCLGetdata;
import ac.ui.controls.PCLGetdataFull;
import ac.ui.controls.PCLImage;
import ac.ui.controls.PCLLabel;
import ac.ui.controls.PCLList;
import ac.ui.controls.PCLMoney;
import ac.ui.controls.PCLPanel;
import ac.ui.controls.PCLSpinner;
import ac.ui.controls.PCLTextField;
import ac.ui.controls.PCLToolbar;
import ac.ui.controls.PCLcomboboxAdd;
import ac.uitls.propertiesOperation;
import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JViewport;
import javax.swing.MenuElement;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.basic.BasicComboPopup;
import org.jdesktop.swingx.JXDatePicker;

/**
 *
 * @author ismail
 */
public class updateitems extends custemItmes {

    propertiesOperation op = new propertiesOperation();
    JToolBar jToolBar2;
    custemItmes cu = new custemItmes();

    //<editor-fold defaultstate="collapsed" desc="Panels Oprtions">
   /* public void panelClaer(Component[] coms) {
     for (Component c : coms) {
     if (c instanceof javax.swing.JTextField) {
     javax.swing.JTextField d = (javax.swing.JTextField) c;
                
     d.setText("");
     }
            
     if (c instanceof javax.swing.JTextArea) {
     javax.swing.JTextArea d = (javax.swing.JTextArea) c;
                
     d.setText("");
     }
            
     }
        
     }*/
    public static void panelClaers(Component[] coms, boolean enableStaus) {
        for (Component c : coms) {

            if (c instanceof ac.ui.controls.PCLTextField) {
                ac.ui.controls.PCLTextField d = (ac.ui.controls.PCLTextField) c;
                d.setEnabled(enableStaus);
                d.setText("");
            } else if (c instanceof JScrollPane) {
                JScrollPane d = (JScrollPane) c;

                JViewport view = d.getViewport();

                panelClaers(view.getComponents(), enableStaus);
            } else if (c instanceof JComboBox) {
                JComboBox d = (JComboBox) c;

                comBoxSetSelectonByValue(d, "");

            } else if (c instanceof JSpinner) {
                JSpinner d = (JSpinner) c;

                d.setValue(0);

            } else if (c instanceof ac.ui.controls.PCLTextarea) {
                ac.ui.controls.PCLTextarea d = (ac.ui.controls.PCLTextarea) c;
                d.setEnabled(enableStaus);
                d.setText("");
            }

        }

    }

    public void panelClaer(Component[] coms, boolean enableStaus) {
        for (Component c : coms) {

            if (c instanceof ac.ui.controls.PCLCode) {
                ac.ui.controls.PCLCode d = (ac.ui.controls.PCLCode) c;
                d.setEnabled(enableStaus);
                d.setCode();
            } else if (c instanceof PCLMoney) {
                PCLMoney d = (PCLMoney) c;

                d.setValue(0);

            } else if (c instanceof ac.ui.controls.PCLTextField) {
                ac.ui.controls.PCLTextField d = (ac.ui.controls.PCLTextField) c;
                d.setEnabled(enableStaus);
                d.setText("");
            } else if (c instanceof PCLcomboboxAdd) {
                PCLcomboboxAdd d = (PCLcomboboxAdd) c;
                d.redef();
            } else if (c instanceof PCLDate) {
                PCLDate d = (PCLDate) c;
                d.setDate(new Date());

            } else if (c instanceof PCLGetdata) {
                PCLGetdata d = (PCLGetdata) c;
                d.redef();

            } else if (c instanceof PCLGetdataFull) {
                PCLGetdataFull d = (PCLGetdataFull) c;
                d.redef();

            } else if (c instanceof JComboBox) {
                JComboBox d = (JComboBox) c;

                comBoxSetSelectonByValue(d, "");

            } else if (c instanceof PCLImage) {
                PCLImage d = (PCLImage) c;
                d.setText("");
                d.setIcon(null);

            } else if (c instanceof JSpinner) {
                JSpinner d = (JSpinner) c;

                d.setValue(0);

            } else if (c instanceof JScrollPane) {
                JScrollPane d = (JScrollPane) c;

                JViewport view = d.getViewport();

                panelClaer(view.getComponents(), enableStaus);
            } else if (c instanceof ac.ui.controls.PCLTextarea) {
                ac.ui.controls.PCLTextarea d = (ac.ui.controls.PCLTextarea) c;
                d.setEnabled(enableStaus);
                d.setText("");
            }

        }

    }

    public static void ComponentsEnable(Component[] coms, boolean enableStaus) {

        for (Component c : coms) {
            if (!(c instanceof PCLToolbar)) {
                c.setEnabled(enableStaus);
                if (c instanceof PCLPanel) {
                    PCLPanel d = (PCLPanel) c;
                    ComponentsEnable(d.getComponents(), enableStaus);
                } else if (c instanceof JTextArea) {
                    JTextArea d = (JTextArea) c;

                } else if (c instanceof PCLToolbar) {

                    PCLToolbar d = (PCLToolbar) c;
                    ComponentsEnable(d.getComponents(), enableStaus);
                }
            }

        }

    }

    public void Panelopen(JPanel parent, PCLPanel chiled) {
        permissions p = new permissions();
        if (chiled.getType() == null || p.isView(chiled.getType(), true)) {
            if (op.getProperty("lang").equals("ar")) {

                chiled.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            }

            parent.removeAll();

            chiled.setSize(parent.getSize());

            parent.add(chiled);
            parent.validate();
            parent.repaint();
        }

    }

    // uiUtil u=new util();
    public void updateLabel(Component c) {

        //  this.sendMessage("khjk");
        if (c instanceof javax.swing.JButton) {
            javax.swing.JButton d = (javax.swing.JButton) c;

            String[] myname = d.getText().split("_");
            String newna = op.getWord("label." + myname[1]);
            //  this.sendMessage(newna+":"+myname[0]);
            if (newna != null && !newna.trim().equals("")) {
                d.setText(newna);
            }

        } else if (c instanceof javax.swing.JLabel) {

            javax.swing.JLabel d = (javax.swing.JLabel) c;

            String[] myname = d.getText().split("_");

            String newna = op.getWord("label." + myname[1]);

            if (newna != null && !newna.trim().equals("")) {

                d.setText(newna);
            }

        } else if (c instanceof javax.swing.JTabbedPane) {
            javax.swing.JTabbedPane d = (javax.swing.JTabbedPane) c;

            for (int i = 0; i < d.getTabCount(); i++) {
                String[] myname = d.getTitleAt(i).split("_");
                String newna = op.getWord("label." + myname[1]);
                d.setTitleAt(i, newna);
            }

            this.updateComponents(d.getComponents());

        } else if (c instanceof javax.swing.JPanel) {
            javax.swing.JPanel d = (javax.swing.JPanel) c;
            this.updateComponents(d.getComponents());

        } else if (c instanceof javax.swing.JToolBar) {
            javax.swing.JToolBar d = (javax.swing.JToolBar) c;
            this.updateComponents(d.getComponents());

        } else if (c instanceof javax.swing.JSplitPane) {
            javax.swing.JSplitPane d = (javax.swing.JSplitPane) c;
            this.updateComponents(d.getComponents());

        }
//comps = c.getComponents();
    }

//</editor-fold>
   /* public void updatemenItems(javax.swing.MenuElement[] ms) {

     for (javax.swing.MenuElement m : ms) {

     if (m instanceof javax.swing.JMenuItem) {
     javax.swing.JMenuItem d = (javax.swing.JMenuItem) m;
     d.setText(getw(d.getText()));
     this.updatemenItems(d.getSubElements());
     } else if (m instanceof javax.swing.JMenu) {
     javax.swing.JMenu d = (javax.swing.JMenu) m;
     d.setText(getw(d.getText()));
     this.updatemenItems(d.getSubElements());
     }
     }

     }
     */
    public void updateInfoValues(Component[] coms) {

        String color
                = op.getProperty("infoValuesColor");
        for (Component c : coms) {
            if (c instanceof javax.swing.JLabel) {
                javax.swing.JLabel d = (javax.swing.JLabel) c;
                d.setForeground(Color.decode("#" + color));

            }
        }
    }

    public void updateInfoLabels(Component[] coms) {

        String color
                = op.getProperty("infoLabelColor");
        for (Component c : coms) {
            if (c instanceof javax.swing.JLabel) {
                javax.swing.JLabel d = (javax.swing.JLabel) c;
                d.setForeground(Color.decode("#" + color));

            }
        }
    }

    public void updatemenItems(JMenu men) {

        for (int i = 0; i < men.getItemCount(); i++) {

            if (men.getItem(i) != null) {

                String otext = men.getItem(i).getText();

                men.getItem(i).setText(getw(otext));
            }

        }

    }

    public void updateComponents(Component[] coms) {

        for (Component c : coms) {
            if (c instanceof JRadioButton) {
                JRadioButton d = (JRadioButton) c;
                d.setText(getw(d.getText()));

            } else if (c instanceof org.jdesktop.swingx.JXTaskPane) {
                org.jdesktop.swingx.JXTaskPane d = (org.jdesktop.swingx.JXTaskPane) c;

                Component[] comsm = d.getComponents();
                Container cont = (Container) comsm[0];

            } else if (c instanceof PCLTextField) {
                PCLTextField d = (PCLTextField) c;
                if (d.isRequired()) {

                    d.setBackground(Color.decode("#" + po.getProperty("requiredBackground")));

                }

            } else if (c instanceof javax.swing.JToolBar) {
                javax.swing.JToolBar d = (javax.swing.JToolBar) c;
                this.updateComponents(d.getComponents());

            } else if (c instanceof ac.ui.controls.PCLGetdataFull) {
                ac.ui.controls.PCLGetdataFull d = (ac.ui.controls.PCLGetdataFull) c;
                if (d.isRequired()) {
                    d.setBackground(Color.decode("#" + po.getProperty("requiredBackground")));
                }
                d.updateData();

            } else if (c instanceof javax.swing.JMenu) {
                javax.swing.JMenu d = (javax.swing.JMenu) c;

                d.setText(getw(d.getText()));

                this.updateComponents(d.getComponents());

                this.updatemenItems(d);

            } else if (c instanceof javax.swing.JMenuBar) {
                javax.swing.JMenuBar d = (javax.swing.JMenuBar) c;
                this.updateComponents(d.getComponents());

            } else if (c instanceof PCLButton) {

                PCLButton d = (PCLButton) c;
                updaetButtons(d);

            } else if (c instanceof PCLCheckBox) {

                PCLCheckBox d = (PCLCheckBox) c;
                d.setText(getw(d.getText()));

            } else if (c instanceof JButton) {

                JButton d = (JButton) c;
                d.setText(getw(d.getText()));

            } else if (c instanceof PCLLabel) {

                PCLLabel d = (PCLLabel) c;
                d.setText(getw(d.getText()));
                if (d.isFontBold()) {

                    d.setFont(new java.awt.Font("Tahoma", 0, 14));
                }

                // NOI18N
            } else if (c instanceof JLabel) {

                JLabel d = (JLabel) c;
                d.setText(getw(d.getText()));

                // NOI18N
            } else if (c instanceof javax.swing.JTabbedPane) {

                javax.swing.JTabbedPane d = (javax.swing.JTabbedPane) c;

                for (int i = 0; i < d.getTabCount(); i++) {
                    String[] myname = d.getTitleAt(i).split("_");
                    if (myname.length > 1) {

                        String newna = op.getWord("label." + myname[1]);
                        d.setTitleAt(i, newna);
                    }
                }
                this.updateComponents(d.getComponents());

            } else if (c instanceof ac.ui.controls.PCLcomboboxAdd) {
                ac.ui.controls.PCLcomboboxAdd d = (ac.ui.controls.PCLcomboboxAdd) c;
                if (d.isRequired()) {
                    d.setBackground(Color.decode("0x" + po.getProperty("requiredBackground")));
                }
                d.updateTypes();

            } else if (c instanceof ac.ui.controls.PCLTree) {
                ac.ui.controls.PCLTree d = (ac.ui.controls.PCLTree) c;

                d.updateData();

            } else if (c instanceof javax.swing.JPanel) {

                JPanel d = (JPanel) c;

                this.updateComponents(d.getComponents());

            } else if (c instanceof javax.swing.JSplitPane) {
                javax.swing.JSplitPane d = (javax.swing.JSplitPane) c;
                this.updateComponents(d.getComponents());

            } else if (c instanceof JScrollPane) {
                JScrollPane d = (JScrollPane) c;

                JViewport view = d.getViewport();
                this.updateComponents(view.getComponents());

            } else if (c instanceof PCLGetdata) {
                PCLGetdata d = (PCLGetdata) c;
                if (d.isRequired()) {
                    Color n = Color.decode("0x" + po.getProperty("requiredBackground"));

                    Color redColor = new Color(n.getRGB());

                    // 
                    d.getEditor().getEditorComponent().setBackground(redColor);

                    ((JTextField) d.getEditor().getEditorComponent()).setOpaque(true);
                    d.setBackground(redColor);
                }
            }

        }
//comps = c.getComponents();
    }

    void updaetButtons(PCLButton d) {

        d.setText(getw(d.getText()));

        ImageIcon closeXIcon;

        Dimension closeButtonSize;
        String image = "";
        String type = d.getType();
        if (type != null && !type.equals("")) {
            switch (type) {
                case "edit":
                    image = "edit.png";
                    break;
                case "delete":
                    image = "delete.png";
                    break;
                case "print":
                    image = "print.png";
                    break;
                case "add":
                    image = "add.png";
                    break;
                case "cancel":
                    image = "cancel.png";
                    break;
                default:
                    image = type + ".png";
                    break;

            }
        }

        if (!image.equals("")) {

            closeXIcon = new ImageIcon(getClass().getResource(URL_Image + image));
            closeButtonSize = new Dimension(closeXIcon.getIconWidth() + 2, closeXIcon.getIconHeight() + 2);

            d.setIcon(closeXIcon);

        }

    }

    public void updateGetDataControl(PCLGetdata gatdat, int id) {
        String[] data = db.getRow(tableAddstree, id);
        ac.ui.utils.comBoxappends(gatdat, new String[]{data[0]}, new String[]{data[1]});

        ac.ui.utils.comBoxSetSelectonByValue(gatdat, data[0]);
    }

    public void DataToComponents(Component[] coms, int IndexID, String[] data, String[] fieds, String TabelString, String[] getDataas) {
        int i = 0;
        for (Component c : coms) {
            if (data[i] != null) {
                if (!data[i].equalsIgnoreCase("noValue")) {
                    if (c instanceof ac.ui.controls.PCLImage) {

                        PCLImage d = (PCLImage) c;

                        ImageIcon imageIcon = db.getimg(TabelString, fieds[i], IndexID);
                        if (data[i] != null && !data[i].equals("")) {
                            d.updateImage(imageIcon, data[0]);
                        }
                    } else if (c instanceof PCLcomboboxAdd) {
                        if (data[i] != null && !data[i].equals("")) {
                            PCLcomboboxAdd d = (PCLcomboboxAdd) c;
                            ac.ui.utils.comBoxSetSelectonByValue(d, data[i]);

                        }
                    } else if (c instanceof PCLList) {
                        if (data[i] != null && !data[i].equals("")) {
                            PCLList d = (PCLList) c;

                            listSetSelectonByValue(d, data[i]);

                        }
                    } else if (c instanceof PCLCheckBox) {
                        PCLCheckBox d = (PCLCheckBox) c;
                        if (data[i].equalsIgnoreCase("1")) {
                            d.setSelected(true);
                        } else {
                            d.setSelected(false);
                        }
                    } else if (c instanceof PCLMoney) {
                        PCLMoney d = (PCLMoney) c;
                        if (data[i] != null && !data[i].equals("")) {
                            d.setValue(new Double(data[i]));
                        }
                    } else if (c instanceof PCLGetdata) {

                        PCLGetdata d = (PCLGetdata) c;
                        if (getDataas != null) {
                            if (getDataas.length > i) {
                                if (!getDataas[i].equalsIgnoreCase("text")) {
                                    d.updateValue(data[i]);
                                }
                            } else {
                                d.updateValue(data[i]);
                            }
                        } else {
                            d.updateValue(data[i]);

                        }
                    } else if (c instanceof PCLGetdataFull) {
                        PCLGetdataFull d = (PCLGetdataFull) c;

                        ac.ui.utils.comBoxSetSelectonByValue(d, data[i]);

                    } else if (c instanceof ac.ui.controls.PCLTree) {

                        ac.ui.controls.PCLTree d = (ac.ui.controls.PCLTree) c;
                        d.selectBYValue(data[i]);

                    } else if (c instanceof JXDatePicker) {

                        JXDatePicker d = (JXDatePicker) c;
                        Date date;
                        try {
                            //  date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(data[i]);
                            String Sdata = data[i];
                            d.setDate(ac.lib.data.dateTime.convertStringFromDBTodate(Sdata));
                            //d.setDate(null);
                        } catch (ParseException ex) {
                            System.out.println(ex.getMessage());
                        }

                    } else if (c instanceof PCLDate) {

                        PCLDate d = (PCLDate) c;
                        Date date;
                        try {
                            //  date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(data[i]);
                            String Sdata = data[i];
                            d.setDate(ac.lib.data.dateTime.convertStringFromDBTodate(Sdata));
                            //d.setDate(null);
                        } catch (ParseException ex) {
                            System.out.println(ex.getMessage());
                        }

                    } else if (c instanceof PCLSpinner) {
                        PCLSpinner d = (PCLSpinner) c;
                        d.setValue(new Double(data[i]));
                    } else if (c instanceof ac.ui.controls.PCLGetdata) {

                        PCLGetdata d = (PCLGetdata) c;

                        updateGetDataControl(d, Integer.parseInt(data[i]));

                    } else if (c instanceof javax.swing.JTextField) {
                        javax.swing.JTextField d = (javax.swing.JTextField) c;
                        d.setText(data[i]);
                    } else if (c instanceof javax.swing.JTextArea) {
                        javax.swing.JTextArea d = (javax.swing.JTextArea) c;
                        d.setText(data[i]);
                    } else if (c instanceof javax.swing.JLabel) {
                        javax.swing.JLabel d = (javax.swing.JLabel) c;
                        d.setText(data[i]);

                    } else if (c instanceof javax.swing.JComboBox) {
                        javax.swing.JComboBox d = (javax.swing.JComboBox) c;
                        ac.ui.custemItmes.comBoxSetSelectonByValue(d, data[i]);

                    }
                }
            }
            i++;
        }
    }

    private String getw(String d) {
        String[] myname = d.split("_");
        String r = d;
        if (myname.length > 1) {

            r = myname[1];

            String newna = op.getWord("label." + myname[1]);

            if (newna != null && !newna.trim().equals("")) {

                r = newna;
            }

        }

        return r;
    }

}
